<template>
	<view class="wrap" style="background-color: #fff; padding: 0 20rpx; min-height: 100vh;">
		<u-tabs :list="listTab" v-if="isListTab" :is-scroll="false" :current="current" @change="change" active-color="#F18B3A"></u-tabs>
		<u-row gutter="16">
			<u-col span="6" v-for="(item,index) in list" :key="index">
				<view class="demo-warter" @click="listRouter(item)">
					<u-image width="100%" height="230rpx" :src="item.icon"></u-image>
					<view class="demo-title">
						{{item.name}}
					</view>
				</view>
			</u-col>
		</u-row>
	</view>
</template>

<script>
	import {baseUrl} from '../../api/index.js'
	import {booksTree,bookTreesCompulsory,bookTreesOptional} from '../../api/api.js'
	export default {
		data() {
			return {
				list: [],
				isListTab:true,
				listTab: [{
					name: '必学课程'
				}, {
					name: '选学课程'
				}],
				current: 0,
				showOptionalCourse:false,
			}
		},
		onShow() {
			this.current = 0
			let user = uni.getStorageSync('userObj')
			if(user && user.profession.name == '游客模式'){
				this.isListTab = false
				this.showOptionalCourse = true
				this.addRandomData()
			}else{
				this.isListTab = true
				this.showOptionalCourse = false
				this.addRandomData()
			}
		},
		onLoad() {
			
		},
		mounted() {
			
		},
		methods: {
			change(index) {
				if(index == 0){
					this.showOptionalCourse = false
				}else{
					this.showOptionalCourse = true
				}
				this.current = index;
				this.addRandomData()
			},
			listRouter(item){
				let itemObj = JSON.stringify(item)
				uni.navigateTo({
					// url:`../list/list?title=${item.name}&id=${item.id}&professionId=${uni.getStorageSync('userObj').profession.id}&showOptionalCourse=${this.showOptionalCourse}`
					url:`../list/list?item=${itemObj}&professionId=${uni.getStorageSync('userObj').profession.id}&showOptionalCourse=${this.showOptionalCourse}`
				})
			},
			addRandomData() {
				this.list = []
				let id = uni.getStorageSync('userObj') ? uni.getStorageSync('userObj').profession.id : ''
				if(this.current == 0){
					bookTreesCompulsory({professionId:id}).then(resp=>{
						resp.forEach(e=>{
							e.icon = baseUrl + e.icon
						})
						this.list = resp
					})
				}else{
					bookTreesOptional({professionId:id}).then(resp=>{
						resp.forEach(e=>{
							e.icon = baseUrl + e.icon
						})
						this.list = resp
					})
				}
			},
		}
	}
</script>

<style>
	/* page不能写带scope的style标签中，否则无效 */
	page {
		background-color: rgb(240, 240, 240);
	}
</style>

<style lang="scss" scoped>
	.demo-warter {
		border-radius: 10rpx;
		margin: 40rpx 20rpx;
		background-color: #ffffff;
		position: relative;
		box-shadow: 0 4px 14px 0 rgba(241, 139, 58, 0.2);
	}
	
	.u-close {
		position: absolute;
		top: 32rpx;
		right: 32rpx;
	}
	
	.demo-image {
		width: 100%;
		border-radius: 4px;
	}
	
	.demo-title {
		height: 140rpx;
		line-height: 50rpx;
		font-size: 36rpx;
		text-align: center;
		color: #000;
		overflow: hidden;
		color: $u-main-color;
		padding: 20rpx 0;
	}
</style>